package com.buba.appointment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.appointment.service.AppointmentService;
import com.buba.appointment.vo.AppointmentVo;
import com.buba.pojo.appointment.Appointment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.buba.appointment.service.impl.AppointmentServiceImpl;
import com.buba.pojo.appointment.Appointment;
import com.buba.pojo.record.WaitingRecord;
import com.buba.utils.R;
import org.springframework.web.bind.annotation.*;



import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预约挂号表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/appointment/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    //取消预约
    @GetMapping("/cancelRegById/{id}")
    public R cancelRegById(@PathVariable Integer id){
        log.info("id:::"+id);
        Appointment appointment = appointmentService.getById(id);
        appointment.setState("已取消");
        appointmentService.updateById(appointment);
        return R.ok();

    }

    //获取挂号记录详情
    @GetMapping("/regRecordById/{id}")
    public R regRecordById(@PathVariable Integer id){
        AppointmentVo appointmentVo = appointmentService.regRecordById(id);
        return R.ok().data("appointment" , appointmentVo);
    }


    //根据日期查询就诊人预约记录
    @GetMapping("/cutPatientDate/{id}/{begin}/{end}")
    public R cutPatientDate(@PathVariable Integer id , @PathVariable String begin , @PathVariable String end){
        log.info("id::"+id);
        log.info("begin::"+begin);
        log.info("end::"+end);

//        String[] split = date.split(",");
        List<AppointmentVo> appointmentVos = appointmentService.cutPatientDate(id , begin , end);
        return R.ok().data("appointment" , appointmentVos);
    }

    //根据就诊人id查询就诊人挂号记录
    @GetMapping("/regPatById/{id}")
    public R regPatById(@PathVariable Integer id){
        List<AppointmentVo> appointmentVos = appointmentService.regPatById(id);
        return R.ok().data("appointment" , appointmentVos);
    }

    @GetMapping({"/addhesuan/{patientId}/{DepartmentId}/{doctorId}/{HospitalId}/{hozhenId}"})
    public R addhesuan(@PathVariable("patientId") Integer patientId, @PathVariable("DepartmentId") Integer DepartmentId, @PathVariable("doctorId") Integer doctorId,
                       @PathVariable("HospitalId") Integer HospitalId,@PathVariable("hozhenId") Integer hozhenId) {
        log.info("patientId==="+patientId);
        log.info("DepartmentId==="+DepartmentId);
        log.info("doctorId==="+doctorId);
        log.info("HospitalId==="+HospitalId);
        log.info("hozhenId==="+hozhenId);

        Appointment appointment = new Appointment();
        appointment.setPatientId(patientId);
        appointment.setDoctorId(doctorId);
        appointment.setCampusId(HospitalId);
        appointment.setDepartmentId(DepartmentId);
        appointment.setState("待就诊");
        appointment.setDeleteFlag("0");
        appointment.setCreateTime(new Date());
        appointment.setUpdateTime(new Date());
        appointment.setHouzhenId(hozhenId);

        boolean save = appointmentService.save(appointment);
        log.info("save=="+save);
        if(save == true){

            int id = appointment.getId();
            log.info("id===="+id);
            return R.ok().data("id",id);
        }else {
            return R.error();
        }

    }

    //
    //根据就诊人id查询就诊人挂号记录
    @GetMapping("/gethouzhen/{id}")
    public R gethouzhen(@PathVariable Integer id){

        log.info("预约id==="+id);
        AppointmentVo appointmentVo = appointmentService.gethouzhen(id);
        if(appointmentVo != null){
            return R.ok().data("appointment" , appointmentVo);
        }else{
            return R.error();
        }

    }

    @PostMapping("/updateghState")
    public R updateghState(@RequestBody Appointment appointment){
        appointment.setState("已完成");
        appointment.setUpdateTime(new Date());
        boolean update=appointmentService.updateById(appointment);
        if(update==true){
            return R.ok().data("ok","修改成功");
        }else {
            return R.ok().data("error","修改失败");
        }
    }

}
